//
//  HomeTimelineCell.swift
//  WBSwift
//
//  Created by 贺瑞 on 2017/8/4.
//  Copyright © 2017年 herui. All rights reserved.
//

import UIKit

class HomeTimelineCell: UITableViewCell {

    @IBOutlet weak var iconIV: UIImageView!
    @IBOutlet weak var nickL: UILabel!
    @IBOutlet weak var vipIV: UIImageView!
    @IBOutlet weak var timeL: UILabel!
    @IBOutlet weak var sourceL: UILabel!
    @IBOutlet weak var contentL: UILabel!
    @IBOutlet weak var retweetContentL: UILabel!
    @IBOutlet weak var retweetBgV: UIView!
    //图片九宫格
    @IBOutlet weak var picV: UICollectionView!
    @IBOutlet weak var consH_picV: NSLayoutConstraint!
//    @IBOutlet weak var consW_picV: NSLayoutConstraint!

    private let margin : CGFloat = 10.0 as CGFloat
    private var itemWH : CGFloat
    {
        return (bounds.size.width - (margin * 6)) / 3
    }
    
    var model : TimelineInfo?
    {
        didSet {
            guard let model = model else {
                return
            }
            let created = Date.date(str: model.created_at, format: "EEE MM dd HH:mm:ss Z yyyy")
            timeL.text = created?.convertToTimelineString()
            contentL.text = model.text
            nickL.text = model.user?.screen_name
            iconIV.sd_setImage(with: URL(str: model.user?.avatar_large), placeholderImage:#imageLiteral(resourceName: "avatar_default_small"))
            
            if let retweeteName = model.retweeted_status?.user?.screen_name {
                retweetContentL.text = "@\(retweeteName): " + (model.retweeted_status?.text ?? "")
            }else {
                retweetContentL.text = nil
            }
            
            retweetBgV.isHidden = (model.retweeted_status == nil)
            
            
            let layout = picV.collectionViewLayout as! UICollectionViewFlowLayout
            layout.itemSize = CGSize(width: itemWH, height: itemWH)

            
            if(model.pic_urls?.count == 0){
                model.pic_urls = model.retweeted_status?.pic_urls;
            }
            
            let count = model.pic_urls?.count ?? 0
            
            switch count {
            case 0:
                consH_picV.constant = 0
                break
            case 4:
                consH_picV.constant = itemWH * 2 + margin
                break
            default:
                let row = (count - 1) / 3 + 1
                consH_picV.constant = CGFloat(row) * itemWH + (CGFloat(row - 1) * margin)
                break
            }
            picV.reloadData()
        }
    }
    
    
    
    override func awakeFromNib() {
                
        picV.register(HomeTimelineCellPicItem.classForCoder(), forCellWithReuseIdentifier: "HomeTimelineCellPicItem")
        picV.dataSource = self
    }
}


extension HomeTimelineCell : UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return model?.pic_urls?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeTimelineCellPicItem", for: indexPath) as! HomeTimelineCellPicItem
        let url = model?.pic_urls?[indexPath.row].thumbnail_pic
        cell.imageV.sd_setImage(with: URL(str:url), placeholderImage: #imageLiteral(resourceName: "empty_picture"))
        return cell
    }
    
}


class HomeTimelineCellPicItem: UICollectionViewCell {
    
    var imageV : UIImageView
    {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        return imageView
    }
}
